package com.c8a.config;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * @Author: C8A
 * @Date: 2021-08-19 9:35
 */

@Component
public class ScheduledTask {
    @Scheduled(cron="0 30 9,18 ? * MON-FRI")
    public void scheduledTask() throws IOException {
        try {
            URL url = new URL("https://qmsg.zendee.cn/send/b48ea33bef257359181db150ceac53f6?msg="+ URLEncoder.encode("主人，海马汇打卡时间到了！","utf-8"));

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //
            // 设置该连接是可以输出的
            connection.setDoOutput(true);
            // 设置请求方式
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            String line = null;
            StringBuilder result = new StringBuilder();
            // 读取数据
            while ((line = br.readLine()) != null) {
                result.append(line).append("\n");
            }
            connection.disconnect();

            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
